<?php
/*
Plugin Name: j-Tag
Plugin URI: http://6ke.me
Description: 通过实现设定的关键字自动生成文章的tag.
Version: 1.0
Author: Liuke
Author URI: http://6ke.me
License: A "Slug" license name e.g. GPL2
*/


/* 判断是否在 WordPress 后台 */
add_action('save_post', 'convert');


function convert($post_id){
		$post	= get_post(wp_is_post_revision($post_id));
		// No title? No point in going any further:
		if(isset($post->post_title)) :
			$content	= $post->post_content;
			// Only run if there are not already tags assigned to the post:
			if(!wp_get_post_tags($post_id)) :
				// Setup our tag data:
				$title_to_tags	= array();
				$tagwords		= getWords();
				foreach ($tagwords as $word) :
					if(strstr($content,$word)):
						$title_to_tags[] = $word;
						file_put_contents(dirname(__FILE__).'/mytext.txt',json_encode($title_to_tags));
					endif;
				endforeach;
				// Finally, add the tags to the post
				wp_add_post_tags($post_id, $title_to_tags);
			endif;
		endif;
		

}

// Gets the stop word list:
	function getWords() {
		$vals			= array();
		$file 			= dirname(__FILE__).'/words.txt';
		$stopwords		= explode(',', file_get_contents($file));
		return $stopwords;
	}
	
?>